<!--
 * @Author: qwlbwm 18370818801qwl./
 * @Date: 2023-09-06 13:36:58
 * @LastEditors: qwlbwm 18370818801qwl./
 * @LastEditTime: 2023-09-09 09:02:03
 * @FilePath: \checc-business-module-web-kgpt-dev\src\views\examineManage\components\editAddStandardIndex.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <!-- 标准指标库的新增（包括中交和中咨） -->
  <div>
    <el-dialog
      v-model="dialogVisible"
      :show-close="false"
      width="1100px"
      :title="opType == 'add' ? '新增' : '编辑'"
      :before-close="beforeClose"
    >
      <div class="examine-dialog-content">
        <el-form
          :inline="true"
          :model="myForm"
          ref="ruleFormRef"
          :rules="rules"
          label-position="top"
          label-width="120px"
        >
          <el-divider content-position="left">基本信息</el-divider>
          <el-form-item label="父级指标编号" prop="parentNumber">
            <!-- <el-cascader
              style="width: 240px"
              :show-all-levels="false"
              v-model="myForm.parentNumber"
              :options="state.standardAllDatas"
              size="small"
              :props="state.cascaderprops"
              clearable
            /> -->
          </el-form-item>
          <el-form-item label="末位编号" prop="endNumber">
            <!-- <el-input v-model="myForm.endNumber" clearable size="small" style="width: 240px" /> -->
          </el-form-item>
          <el-form-item label="指标类型" prop="indexType">
            <!-- <el-select v-model="myForm.indexType" clearable style="width: 240px" size="small">
              <el-option
                v-for="item in state.dict_indexTypes"
                :key="item.itemValue"
                :label="item.itemText"
                :value="item.itemValue"
              >
              </el-option>
            </el-select> -->
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="confirm()"> 保存 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
    
    <script lang='ts'>
import { computed, ref, defineComponent, reactive, watch, toRefs, nextTick } from 'vue'
import { ElMessage } from 'element-plus'
import * as api from '../../../api/examineManage'
import pubFc from '../../../utils'
export default defineComponent({
  components: {},
  emits: {
    'update:visible': null,
    refresh: null,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    opType: {
      //操作类型新增或编辑
      type: String,
      default: 'add',
    },
    editForm: {
      type: Object, //1中交还是2中咨
    },
  },
  setup(props, { emit }) {
    //表格用到的参数
    // let {standardType,opType}=props;
    const dialogVisible = computed(() => {
      return props.visible
    })
    const ruleFormRef = ref<any>()
    const rules = reactive<any>({
      //   parentNumber: [{ required: true, message: '请选择父级', trigger: 'blur' }],
      endNumber: [{ required: true, message: '请填写编号', trigger: 'blur' }],
    })
    const state = reactive<any>({
      //整体数据带dict全是字典查询

    })
    const formBackData = {

    }
    const myForm = reactive({ ...formBackData })
    watch(
      () => props.visible,
      (newval) => {
        if (newval) {
          nextTick(() => {
            //初始绑定需在form完成初始化
            ruleFormRef.value?.resetFields()
            if (props.opType == 'edit' || props.opType == 'copy') {
              // console.log('props.editForm',props.editForm);
              pubFc.ojectCopyObj(myForm, props.editForm)
              
            }
          })
        }
      }
    )
    const beforeClose = () => {
      cancel()
    }
    const cancel = () => {
      emit('update:visible', false)
    }
    const confirm = () => {
      //确定新增或修改
      ruleFormRef.value.validate((valid) => {
        if (valid) {
          dealParams()
          let apiName = '',
            params = {
              ...myForm,
              id: '',
            }
          if (props.opType === 'add' || props.opType === 'copy') {
            apiName = 'addStandardIndexRequest'
          } else {
            apiName = 'editStandardIndexRequest'
            params.id = props.editForm?.id
          }

          api[apiName](params).then((res) => {
            if (res.data.success) {
              ElMessage({
                message: props.opType === 'edit' ? '成功修改' : '成功创建',
                type: 'success',
              })
              cancel()
              emit('refresh')
            }
          })
        }
      })
    }
    const dealParams = () => {
      //处理新增或编辑前的参数
     console.log('处理参数');
    }

    return {
      ...toRefs(state),
      myForm,
      dialogVisible,
      rules,
      ruleFormRef,
      cancel,
      confirm,
      beforeClose,
    }
  },
})
</script>
    <style lang='scss' scoped>
.examine-dialog-content {
  padding: 20px;
}
</style>
    